﻿using MemorySiteServer.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace MemorySiteServer.ViewModels.Account
{
    public class LoginResponse
    {
        /// <summary>
        /// Token
        /// </summary>
        public string Token { get; set; }
        /// <summary>
        /// 用户信息
        /// </summary>
        public UserInfo UserInfo { get; set; }
    }

    public class UserInfo
    {
        public long ID { get; set; }
        /// <summary>
        /// 用户名
        /// </summary>
        [Required]
        [StringLength(20, MinimumLength = 3)]
        public string Name { get; set; }
        /// <summary>
        /// 角色名称
        /// </summary>
        [Required]
        [MaxLength(20)]
        public string RoleName { get; set; }
        /// <summary>
        /// 角色权限等级
        /// </summary>
        public int RoleLevel { get; set; }
        /// <summary>
        /// 控制器列表
        /// </summary>
        public List<string> Actions { get; set; }

        public static UserInfo GetFromUser(User user)
        {
            return new UserInfo()
            {
                ID = user.ID,
                Name = user.Name,
                RoleName = user.Role.Name,
                RoleLevel = user.Role.Level,
                Actions = user.Role.RolePermissions.Select(x => x.Action).ToList(),
            };
        }

    }

    public class LoginFailResponse
    {
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 验证码图片（base64格式）
        /// </summary>
        public string Code { get; set; }

        public string ToJsonString()
        {
            return Util.SerializeJson(this);
        }
    }
}
